export class UInt8ArrayEx {

    static IsSame(arr1: Uint8Array, arr2: Uint8Array): boolean {
        if (arr1.length != arr2.length) return false;
        for (let i = 0; i < arr1.length; ++i) {
            if (arr1[i] != arr2[i]) {
                return false
            }
        }
        return true;
    }

    static ArrayToStr(arr: Uint8Array): string {
        let CHUNK_SIZE = 0x8000; //arbitrary number
        let index = 0;
        let length = arr.length;
        let result = '';
        let slice;
        while (index < length) {
            slice = arr.subarray(index, Math.min(index + CHUNK_SIZE, length));
            result += String.fromCharCode.apply(null, slice);
            index += CHUNK_SIZE;
        }

        return btoa(result);
    }

    static StrToArr(str: string): Uint8Array {
        let padding = '='.repeat((4 - str.length % 4) % 4);
        let base64 = (str + padding)
            .replace(/\-/g, '+')
            .replace(/_/g, '/');

        let rawData = window.atob(base64);
        let outputArray = new Uint8Array(rawData.length);

        for (var i = 0; i < rawData.length; ++i) {
            outputArray[i] = rawData.charCodeAt(i);
        }
        return outputArray;
    }

}